package com.jqb.ajax.servlet;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by IntelliJ IDEA.
 * User: jqbxd
 * Date: 2009-4-2
 * Time: 23:25:47
 * xml
 */
public class AjaxXMLServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*修改点1---响应的Content-Type必须是text/xml*/
        response.setContentType("text/xml; charset=utf-8");
        PrintWriter out = response.getWriter();
        String name = request.getParameter("name");
//         System.out.println("name="+name);
        /*修改点2---返回的数据需要拼装成xml格式*/
        StringBuilder builder = new StringBuilder();
        builder.append("<message>");
        if (name == null || name.equals("")) {
            builder.append("用户名不能为空!  请重新输入!").append("</message>");
        } else {
            if (name.equals("jqb")) {
                builder.append("欢迎[" + name + "]登录!").append("</message>");
            } else {
                builder.append("用户[" + name + "]不存在!").append("</message>");
            }
        }
        out.println(builder.toString());
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
